package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.Itinerary;

public class ListItineraryAdapter extends ArrayAdapter<Itinerary> {
	private Activity context = null;
	private int resource;
	private ArrayList<Itinerary> arr = null;

	// variable for imageloader
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	public ListItineraryAdapter(Activity context, int resource,
			ArrayList<Itinerary> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.arr = objects;

		// setup image loader
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getContext()).build();

		if (ImageLoader.getInstance().isInited()) {
			ImageLoader.getInstance().destroy();
		}
		ImageLoader.getInstance().init(config);
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_photos)
				.showImageForEmptyUri(R.drawable.no_image)
				.showImageOnFail(R.drawable.no_image).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// set view for list view item
		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(resource, null);
		Itinerary itinerary = arr.get(position);

		ImageView itiImage = (ImageView) convertView
				.findViewById(R.id.itiImage);
		TextView itiTitle = (TextView) convertView.findViewById(R.id.itiTitle);
		ImageView itiQuality = (ImageView) convertView
				.findViewById(R.id.itiQuality);

		// set place icon
		imageLoader.displayImage(arr.get(position).getIconPath(), itiImage,
				options, null);

		// set quality icon
		switch (itinerary.getQuality()) {
		case 5:
			itiQuality.setImageResource(R.drawable.ic_five_star);
			break;
		case 4:
			itiQuality.setImageResource(R.drawable.ic_four_star);
			break;
		case 3:
			itiQuality.setImageResource(R.drawable.ic_three_star);
			break;
		case 2:
			itiQuality.setImageResource(R.drawable.ic_two_star);
			break;
		case 1:
			itiQuality.setImageResource(R.drawable.ic_one_star);
			break;
		default:
			itiQuality.setImageResource(R.drawable.ic_zero_star);
			break;
		}

		// set category
		itiTitle.setText(itinerary.getItineraryName());

		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	public void setArr(ArrayList<Itinerary> arr) {
		this.arr = arr;
	}

}
